package com.fly.caihong.frag;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.fly.caihong.R;
import com.fly.caihong.activity.ChannelActivity;
import com.fly.caihong.base.BaseFragment;
import com.fly.caihong.model.ChannelAllItem;
import com.fly.caihong.model.ChannelItem;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.tab.SlidingTabLayout;
import com.fly.caihong.view.tab.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HomeFragment extends BaseFragment implements OnTabSelectListener, ViewPager.OnPageChangeListener, View.OnClickListener {

    private static final String[] BASE_TABS = {"关注", "推荐", "拍卖", "商城"};
    private List<String> mTabList = new ArrayList();
    private SlidingTabLayout mTabLayout;
    private ViewPager mViewPager;
    private View mIvOption;
    private MyAdapter mAdapter;

    @Override
    protected void initView(View view) {
        mTabLayout = view.findViewById(R.id.tl_tab);
        mViewPager = view.findViewById(R.id.vp_home);
        mIvOption = view.findViewById(R.id.iv_option);
        mTabList.addAll(Arrays.asList(BASE_TABS));
        mAdapter = new MyAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setViewPager(mViewPager, mTabList.toArray(new String[]{}));
        mTabLayout.setCurrentTab(0);
    }

    @Override
    protected void initListener() {
        mTabLayout.setOnTabSelectListener(this);
        mViewPager.addOnPageChangeListener(this);
        mIvOption.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        ChannelAllItem channelAllItem = DataHandler.convertJsonData("home/recommend/channel", ChannelAllItem.class);
        if (channelAllItem == null) {
            ToastUtils.showLong("频道数据为空");
            return;
        }
        ChannelActivity.mLoveList = channelAllItem.getLoveChannel();
        ChannelActivity.mRecList = channelAllItem.getRecommendChannel();
        refreshNewData();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void onTabSelect(int position) {
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_option) {
            startActivityForResult(new Intent(getActivity(), ChannelActivity.class), 189);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 189 && resultCode == ChannelActivity.RESULT_OK) {
            //刷新新选择
            refreshNewData();
            //保存已选的数据
            ChannelAllItem channelAllItem = new ChannelAllItem();
            channelAllItem.setLoveChannel(ChannelActivity.mLoveList);
            channelAllItem.setRecommendChannel(ChannelActivity.mRecList);
            DataHandler.saveJsonData("home/recommend/channel", channelAllItem);
        }
    }

    private void refreshNewData() {
        mTabList.clear();
        List<ChannelItem> dataList = ChannelActivity.mLoveList;
        List<String> newList = new ArrayList<>();
        for (ChannelItem channelItem : dataList) {
            newList.add(channelItem.getName());
        }
        mTabList.addAll(newList);
        mTabLayout.setViewPager(mViewPager, mTabList.toArray(new String[]{}));
        mAdapter.notifyDataSetChanged();
    }

    private class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return new HomeAttentionFragment();
            } else if (position == 1) {
                return new HomeRecommendFragment();
            } else if (position == 2) {
                return new HomeAuctionFragment();
            } else if (position == 3) {
                return new HomeMallFragment();
            } else {
                return new HomeRecommendFragment();
            }
        }

        @Override
        public int getCount() {
            return mTabList.size();
        }
    }
}
